var qcloud = require('../../../../vendor/sdk/index');
var config = require('../../../../config');
var app = getApp();

Page({
  data: {
    navH: app.globalData.navHeight,
    fileHost: app.globalData.imageUrl,
    appletCodeUuid: '',
    wxCodeUuid: ''
  },
  onLoad: function (options) {
    let data = options.data;
    let codeObj = JSON.parse(data);
    console.log('codeObj', codeObj);
    this.setData({
      appletCodeUuid: this.data.fileHost + codeObj.appletCodeUuid,
      wxCodeUuid: this.data.fileHost + codeObj.wxCodeUuid
    });
  },
  onUnload() {
    wx.navigateBack(2);
  },
  onNavBack() {
    // 返回
    wx.navigateTo({
      url: '../../personalPage'
    });
  },
  saveImg() {
    let self = this;
    wx.authorize({
      scope: 'scope.writePhotosAlbum',
      success(res) {
        console.log('authorize', res);
        self.downloadFile(self.data.appletCodeUuid);
        self.downloadFile(self.data.wxCodeUuid);
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        });
      },
      fail() {
        wx.showToast({
          title: '请授权保存到相册',
          icon: 'none',
          duration: 2000
        });
        setTimeout(() => {
          wx.openSetting();
        }, 2000);
      }
    });
  },
  downloadFile(url) {
    wx.downloadFile({
      url: url,
      success: function (res) {
        console.log('download', res);
        let path = res.tempFilePath;
        // 保存到相册
        wx.saveImageToPhotosAlbum({
          filePath: path,
          success: function (res) {
            console.log('saveImageToPhotosAlbum', res);
          },
          fail: function (res) {
            console.log('fail', res);
          }
        });
      }
    });
  }
});
